package com.example.rentingapp.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationHouseEntity;
import com.example.rentingapp.enetity.InformationHouseResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HousingDetailsActivity2 extends BaseActivity implements View.OnClickListener {
    private ImageView iv;
    private Button update_house_btn;
    private TextView landlord_address, rent_data, area_data, type_data, type_data2, landlord_renovation, landlord_description, landlord_location,
            landlord_name, landlord_sex, landlord_phone;
    private ImageView img_house;
    String house_id;

    private List<InformationHouseEntity> datas = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    landlord_address.setText(datas.get(0).getAddress());
                    rent_data.setText(String.valueOf(datas.get(0).getHouse_rent()));
                    area_data.setText(String.valueOf(datas.get(0).getArea()));
                    type_data.setText(String.valueOf(datas.get(0).getHome_type()));
                    type_data2.setText(String.valueOf(datas.get(0).getTime_type()));
                    landlord_description.setText(datas.get(0).getOther());
                    landlord_renovation.setText(datas.get(0).getRenovation());
                    landlord_location.setText(datas.get(0).getLocation());

                    landlord_name.setText(datas.get(0).getName());
                    landlord_sex.setText(datas.get(0).getSex());
                    landlord_phone.setText(datas.get(0).getPhone());


                    Bitmap bitmap = null;
                    try {
                        byte[] bytes = (datas.get(0).getHouse_images()).getBytes("UTF-8");
                        byte[] bitmapArray = Base64.decode(bytes, Base64.DEFAULT);
                        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    img_house.setImageBitmap(bitmap);
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_housing_details2;
    }

    @Override
    protected void initView() {
        iv = findViewById(R.id.iv_back_to_fragment_search);
        update_house_btn=findViewById(R.id.btn_house_update);
        landlord_address = findViewById(R.id.tv_landlord_address);
        landlord_location = findViewById(R.id.tv_landlord_location);
        rent_data = findViewById(R.id.tv_rent_data);
        area_data = findViewById(R.id.tv_area_data);
        type_data = findViewById(R.id.tv_type_data);
        type_data2 = findViewById(R.id.tv_type_data2);
        landlord_renovation = findViewById(R.id.tv_renovation_data);
        landlord_description = findViewById(R.id.tv_landlord_description);

        landlord_name = findViewById(R.id.tv_landlord_name);
        landlord_sex = findViewById(R.id.tv_landlord_sex);
        landlord_phone = findViewById(R.id.tv_landlord_phone);

        img_house = findViewById(R.id.img_house);
    }

    @Override
    protected void initData() {
        iv.setOnClickListener(this);//返回上一级页面
        update_house_btn.setOnClickListener(this);
        house_id = findByKey("house_id");
        getHousingDetails(house_id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_to_fragment_search:
                removeByKey("house_id");
                navigateTo(MyHouseCheckActivity.class);
                finish();
                break;

            case R.id.btn_house_update:
                navigateTo(NewHouseInformationActivity.class);
                finish();
                break;
        }
    }

    public void getHousingDetails(String house_id) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_id", house_id);
        Api.config(ApiConfig.FIND_INFORMATION_HOUSE, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationHouseResponse response = gson.fromJson(res, InformationHouseResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationHouseEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("查看房源详情失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}